package com.hemaapp.yjnh.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hemaapp.yjnh.activity.SearchActivity;
import com.hemaapp.hm_FrameWork.HemaAdapter;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.Blog;

public class SearchHistoryAdapter extends HemaAdapter {

	private static final int TYPE_SEARCH = 0;
	private static final int TYPE_CLEAR = 1;
	private Context mContext;
	private ArrayList<String>histories;
	OnHistoryClickListener mListener;

	public SearchHistoryAdapter(Context mContext,ArrayList<String>histories, OnHistoryClickListener listener) {
		super(mContext);
		this.mContext=mContext;
		this.histories=histories;
		this.mListener = listener;
	}

	@Override
	public int getCount() {
		return histories.size()==0?0:histories.size()+1;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}
	@Override
	public int getItemViewType(int position) {
		if (position==histories.size()) {
			return TYPE_CLEAR;
		} else {
			return TYPE_SEARCH;
		}
	}
	@Override
	public int getViewTypeCount() {
		return 2;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		int type=getItemViewType(position);
		if(convertView==null){
			switch (type) {
			case TYPE_CLEAR:
				convertView=LayoutInflater.from(mContext).inflate(R.layout.item_clearhistory, null);
				ViewHolder holder=new ViewHolder();
				holder.clear=(TextView) convertView.findViewById(R.id.clear);
				convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
				break;
			case TYPE_SEARCH:
				convertView=LayoutInflater.from(mContext).inflate(R.layout.item_searchhistory, null);
				HViewHolder hHolder=new HViewHolder();
				findView(convertView, hHolder);
				convertView.setTag(R.id.TAG_VIEWHOLDER,hHolder);
				break;
			default:
				break;
			}
		}
		
		if(position==histories.size()){
			ViewHolder holder=(ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
			holder.clear.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if (mListener != null)
						mListener.OnClearClickListener();
				}
			});
		}else{
			HViewHolder hHolder=(HViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
			String name=histories.get(position);
			hHolder.search_name.setText(name);
			convertView.setTag(name);
			convertView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					String sea = (String)v.getTag();
					if (mListener != null)
						mListener.OnItemClickListener(sea);
				}
			});
		}
		return convertView;
	}

	private void findView(View convertView, HViewHolder hHolder) {
		hHolder.search_name=(TextView) convertView.findViewById(R.id.search_name);
	}

	private class  ViewHolder {
		private TextView clear;
	}
	private class HViewHolder{
		private TextView search_name;
	}

	public interface OnHistoryClickListener {
		void OnItemClickListener(String key);
		void OnClearClickListener();
	}
}
